package xwbank.jmeter5.scripts.tcpsocket.core;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import xwbank.jmeter5.utils.SocketClientUtil;

/**
 * created by by buxd on 2021-02-23
 * tcpSocket协议的脚本开发
 */

public class CoreServiceTest extends AbstractJavaSamplerClient {
    private static SocketClientUtil socketClientUtil=null;

    @Override
    public void setupTest(JavaSamplerContext context) {
        socketClientUtil = new SocketClientUtil();
    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("ip","");
        params.addArgument("port","");
        params.addArgument("reqStr","");
        return params;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult results = new SampleResult();
        String ipAddress = javaSamplerContext.getParameter("ip");
        String port = javaSamplerContext.getParameter("port");
        String data = javaSamplerContext.getParameter("reqStr"); // 在beanshell中将xml报文复制给变量reqStr
        results.sampleStart();
        String returnInfo = socketClientUtil.sendXmlMsg(data,ipAddress,port);

        if (returnInfo.contains(">Success<")){
            results.setSuccessful(true);
            results.setResponseHeaders(returnInfo);
            results.setRequestHeaders(data);
            results.setDataType("TEXT");
        }else {
            results.setSuccessful(true);
            results.setResponseHeaders(returnInfo);
            results.setRequestHeaders(data);
            results.setDataType("TEXT");
        }
        results.sampleEnd();
        return results;
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }
}
